<!DOCTYPE html>
<html>
  <head>
    <title>退换货</title>
    <meta charset="UTF-8">
	<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
	<link rel="stylesheet" type="text/css" href="css/style.css">
  </head>
  <body>
    <header class="navbar-fixed-top">
		<div class="navbar-header">
			<span class="glyphicon glyphicon-option-vertical"></span>
			<label>服装店Pos管理系统</label>
		</div>
		<div>
			<ul class="nav navbar-nav navbar-right">
				<li><a>欢迎你！</a></li>
				<li><a href="#">个人信息</a></li>
				<li><a id="loginOrOut" href="" ></a></li>
			</ul>
		</div>
	</header>
	<nav class="navbar navbar-default nav-bgcolor navbar-fixed-top nav-vertical">
		<div class="container-fluid">
			<ul class="nav nav-stacked">
				<li><a href="storehouse.html" >商品管理</a></li>
				<li><a href="order.html">订单管理</a></li>
				<li><a href="#">会员管理</a></li>
				<li id="showQuanXian"><a href="quanxian.html">员工管理</a></li>
				<li><a href="cashier.html">支付</a></li>
			</ul>
		</div>
	</nav>
	<main>
		<div id="nav-tab">
			<div id="order-table" class="table">
				<table align="center" class="table table-striped table-hover" >
					<thead>
						<tr>
							<th style="display:none">id</th><th>会员id</th><th>支付方式</th><th>交易数量</th><th>交易时间</th><th>操作</th>
						</tr>
					</thead>
					<tbody id="orderList">
					</tbody>
				</table>
			</div><!-- /.#order-table -->
		</div>
	</main>
	
	<!-- 显示订单详情 -->
	<div id="showOrderItem" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="search orderItem" aria-hidden="true" data-backdrop="static">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" data-dismiss="modal" class="close" onclick="resetOrderItemList();">&times;</button>
					<h4>订单详情</h4>
				</div><!-- /.modal-header -->
				<div class="modal-body">
					<table id="orderItemlList" class="table table-head" >
						<colgroup>
							<col width="20%"/><col width="20%"/><col width="20%"/><col width="15%"/><col width="25%"/>
						</colgroup>
						<thead>
							<tr>
								<th style="display:none">orderItemId</th><th style="display:none">detailId</th><th style='text-align:center'>品名</th><th>数量</th><th>总价</th><th>状态</th><th id="btn-cz" style="text-align:center">操作</th>
							</tr>
						</thead>
						<tbody  class="table" >
									
						</tbody>
					</table>
				</div><!-- /.modal-body -->
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal" onclick="resetOrderItemList();">完成</button>
				</div><!-- /.modal-footer -->
			</div><!-- /.modal-content -->
		</div>
	</div>	
	<!-- 换货选择框 -->
	<div id="chooseGoodToChange" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="search goodDetail" aria-hidden="true" >
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" data-dismiss="modal" class="close" onclick="resetgoodDetailList()">&times;</button>
					<h4>商品详情</h4>
				</div><!-- /.modal-header -->
				<div class="modal-body">
					<table id="goodDetailList" class="table table-head">
						<colgroup>
							<col width="23%"/><col width="13%"/><col width="13%"/><col width="13%"/><col width="10%"/><col width="10%"/><col width="10%"/><col width="14%"/>
						</colgroup>
						<thead>
							<tr>
								<th style="display:none">detailId</th><th style='text-align:center'>品名</th><th>价格</th><th>尺寸</th><th>年龄段</th><th>颜色</th><th>性别</th><th>库存</th><th style='text-align:center'>操作</th>
							</tr>
						</thead>
						<tbody  class="table">
							
						</tbody>
					</table>
				</div><!-- /.modal-body -->
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal" onclick="resetgoodDetailList()">完成</button>
				</div><!-- /.modal-footer -->
			</div><!-- /.modal-content -->
		</div>
	</div>
	<script src="js/jquery-3.2.1.min.js"></script>
	<script src="js/bootstrap.min.js"></script>
	<script type="text/javascript">
		$(document).ready(function(){
			
			$.ajax({
				url : '/ClothesShopPOS/ordertot/list',
				type : "get",
				cache : false,
				success : function(data) {
					var resultList=JSON.parse(data).result;
					var table=$("#orderList");
					
					for(var i in resultList){
						var temp=$("<tr><td style='display:none;'>"+resultList[i].id+"</td>"
						+"<td>"+resultList[i].vipId+"</td>"
						+"<td>"+resultList[i].payWay+"</td>"
						+"<td>"+resultList[i].itemNum+"</td>"
						+"<td>"+timeChange(resultList[i].createTime)+"</td>"
						+"<td><button type='button' class='btn btn-default' onclick='showOrderDetail(this);'>详情</button></tr>"
						);
						temp.appendTo(table);
					}
				}
			})
			
			$.ajax({
				url : '/ClothesShopPOS/log/check_login',
                type: "get",
                cache : false,
                success : function(data) {
                    console.log(data);
                    data = JSON.parse(data);
                    if (data=='0'){
                        $("#loginOrOut").html("登录");
                        $("#loginOrOut").attr("href","login.html");
                    }else if(data=='1'){
                       $("#loginOrOut").html("退出");      
                       $("#loginOrOut").click(function(){
                       		if(confirm("确认退出吗？")){
                       			$.ajax({
		                       		url : '/ClothesShopPOS/log/logout',
					                type: "post",
					                cache : false,
					                success : function(data) {
					                	console.log(data);
		                    			data = JSON.parse(data);
		                    			if(data.statusCode=='0'){
		                    				alert("退出成功");
		                    				window.location.href="login.html";
		                    			}else{
		                    				alert("失败");
		                    			}
					                }
		                     	}) 
                       		}
                       });                
                	}
                }
			})
			
			$.ajax({
				url : '/ClothesShopPOS/log/check_superadmin',
                type: "get",
                cache : false,
                success : function(data) {
                	console.log(data);
           			data = JSON.parse(data);
           			if(data=='0'){
           				//alert("成功");
           				$("#showQuanXian").remove();
           			}
                }
			})
	
		});
	   
	   /*时间戳转为字符串 todo */
    	function timeChange(timestamp){
    		var dateObj=new Date(timestamp);//根据时间戳生成的时间对象
    		var date=(dateObj.getFullYear())+"-"+(dateObj.getMonth()+1)+"-"+(dateObj.getDate())+"-"
    		+(dateObj.getHours())+":"+(dateObj.getMinutes())+":"+(dateObj.getSeconds());
    		return(date);
    		alert(date);
    		
    	}	
    	
    	
    	/**在orderItemList中点击关闭按钮后，清空orderItemList**/
		function resetOrderItemList(){
			$("#showOrderItem tbody").html("");
			
		}
		
		function resetgoodDetailList(){
			$("#goodDetailList tbody").html("");
		}
		
		
		/*显示对应的orderItem  */
		function showOrderDetail(obj){
			$(obj).attr("data-target","#showOrderItem");
	    	$(obj).attr("data-toggle","modal");
	    	var id=$(obj).parent().parent().find("td").eq(0).text();
	    	var table=$("#showOrderItem tbody");
		    $.ajax({
		    	url:'/ClothesShopPOS/orderitem/get_by_ordertot_id',
		    	type : "get",
				cache : false,
				dataType:"json",
				data:{
					orderTotId:id//todo
				}, 
				success:function(data){
					console.log(data);
					var resultOrderItem=JSON.parse(data).result;
				
					for(var i in resultOrderItem){
						var temp=$("<tr><td style='display:none'>"+resultOrderItem[i].id+"</td>"
						+"<td style='display:none'>"+resultOrderItem[i].detailId+"</td>"
						+"<td style='text-align:center'>"+resultOrderItem[i].title+"</td>"
						+"<td>"+resultOrderItem[i].num+"</td>"
						+"<td>"+resultOrderItem[i].receipt+"</td>"
						+"<td>"+resultOrderItem[i].status+"</td>"
						+"<td style='display:none'>"+resultOrderItem[i].orderTotId+"</td>"
						+"<td><button type='button'  class='btn btn-info btn-sm' data-toggle='modal' data-target='#chooseGoodToChange' onclick='showOtherGoodDetail(this)' style='margin:0 5px 0 13px;'>换货</button>"
						+"<button type='button' id='btn-return' class='btn btn-default btn-sm btn-return' onclick='returnGood(this)'>退货</button></td></tr>"
						+"<tr><td colspan='5'>种类:"+resultOrderItem[i].clazz+"&nbsp;&nbsp;&nbsp;|&nbsp;品牌:"
						+resultOrderItem[i].brand+"&nbsp;&nbsp;&nbsp;|&nbsp;颜色:"
						+resultOrderItem[i].color+"&nbsp;&nbsp;&nbsp;|&nbsp;尺寸:"
						+resultOrderItem[i].size+"&nbsp;&nbsp;&nbsp;|&nbsp;性别:"
						+resultOrderItem[i].gender+"&nbsp;&nbsp;&nbsp;|&nbsp;年龄段:"
						+resultOrderItem[i].age+"</td></tr>");
						
						temp.appendTo(table);
					}
				}
		    })
		}
	    
	    /* 退货 */
	    function returnGood(obj){
	    	var id=$(obj).parent().parent().find("td").eq(0).text();
	    	var status=$(obj).parent().parent().find("td").eq(5).text();
	    	if(status == "已退货"){
	    		alert("不能执行此操作！");
	    		$(obj).removeClass("btn-default");
	    		$(obj).addClass("disable");
	    		$(obj).attr("disabled",true);
	    	}else{
	    		if(confirm("确定要退货吗？")){
		    		$.ajax({
		    			url:'/ClothesShopPOS/orderitem/return',
		    			type : "POST",
						cache : false,
						dataType:"json",
						data:{
							id:id
						}, 
						success:function(data){
							console.log(data);
		                    //data = JSON.parse(data);
						    if (data.statusCode=='0'){
						    	 alert("退货成功");
						    	 $(obj).removeClass("btn-default");
					    	     $(obj).addClass("disable");
					    		 $(obj).attr("disabled",true);
						    }else if(data.statusCode=='11'){
						    	alert("您的权限不够，不能执行此操作！");
						    }else{
						    	 alert("退货失败");
						    }
						    $("#showOrderItem").modal('hide');
						}
		    		})
		    	}// --if(confirm("确定要退货吗？"))
	    	}
	    }
	    
	    /*显示换货 模态框 */
	    function showOtherGoodDetail(obj){
	    	var detailId=$(obj).parent().parent().find("td").eq(1).text();
	    	var itemId=$(obj).parent().parent().find("td").eq(0).text();
	    	var orderTotId=$(obj).parent().parent().find("td").eq(6).text();
	    	var status=$(obj).parent().parent().find("td").eq(5).text();
	    	if(status == "已退货"){
	    		alert("不能执行此操作！");
	    		$(obj).removeClass("btn-info");
	    		$(obj).addClass("disable");
	    		$(obj).attr("disabled",true);
	    	}else{
	    		$.ajax({
		    		url:'/ClothesShopPOS/gooddetail/get_by_id',
	    			type : "get",
					cache : false,
					dataType:"json",
					data:{
						id:detailId
					}, 
					success:function(data){
						
						console.log(data);
	                    resultGoodDetail=JSON.parse(data).result;
	                    var goodId=resultGoodDetail.goodId;
	                    
	                    $.ajax({
							url : '/ClothesShopPOS/good/get_by_id',
							type : "get",
							cache : false,
							dataType:"json",
							data:{
								id:goodId
							},
							success:function(data){
								console.log(data);
								var table=$("#chooseGoodToChange #goodDetailList tbody");
								var resultGood=JSON.parse(data).result;
								//alert(typeof resultGoodDetail);
							    //alert(orderTotId);
							    var clazzId=resultGood.clazzId;
							    var brandId=resultGood.brandId;
								
								$.ajax({
							    	url:'/ClothesShopPOS/clazz/get',
							    	type : "get",
									cache : false,
									dataType:"json",
									data:{
										id:clazzId
									},
									success:function(data){
										console.log(data);
										var resultClazz=JSON.parse(data).result;
										var tempClazz=$("<tr><td style='display:none'>"+resultClazz.name+"</td></tr>");
										tempClazz.appendTo(table);
									}
							    })// 通过clazzId查找clazz表获取clazzName
							    
							    $.ajax({
							    	url:'/ClothesShopPOS/brand/get',
							    	type : "get",
									cache : false,
									dataType:"json",
									data:{
										id:brandId
									},
									success:function(data){
										console.log(data);
										var resultBrand=JSON.parse(data).result;
										var tempBrand=$("<tr><td style='display:none'>"+resultBrand.name+"</td></tr>");
										tempBrand.appendTo(table);
									}
							    })// 通过brandId查找brand表获取brandName
								
							    $.ajax({
							    	url:'/ClothesShopPOS/gooddetail/get_by_goodid',
							    	type : "get",
									cache : false,
									dataType:"json",
									data:{
										goodId:goodId
									},
									success:function(data){
										console.log(data);
										var resultGoodDetail=JSON.parse(data).result;
										var temp1=$("<tr><td style='display:none'>"+itemId+"</td>"
										+"<td style='display:none'>"+orderTotId+"</td></tr>");
										temp1.appendTo(table);
										for(var i=0;i<resultGoodDetail.length;i++){
											var temp=$("<tr><td style='display:none'>"+resultGoodDetail[i].id+"</td>"
											+"<td style='text-align:center'>"+resultGood.title+"</td>"
											+"<td>"+resultGoodDetail[i].referencePrice+"</td>"
											+"<td>"+resultGoodDetail[i].size+"</td>"
											+"<td>"+resultGoodDetail[i].age+"</td>"
											+"<td>"+resultGoodDetail[i].color+"</td>"
											+"<td>"+resultGoodDetail[i].gender+"</td>"
											+"<td>"+resultGoodDetail[i].stock.count+"</td>"
											+"<td rowspan='2'><button type='button' class='btn btn-default' onclick='changeGood(this);'>选择</button></td></tr>"
											+"<tr><td style='text-align:center'>"+resultGood.goodNo+"</td></tr>");
											
											temp.appendTo(table);
										}
									}
							    })//第三个ajax结束  通过goodId查找gooddetail表 
							    
							    
							}
						})//第二个ajax结束 通过goodId查找good表
					}
		    	})//第一个ajax结束 通过detailId找goodId
	    	}
	    }
	    
	    /* 换货 */
	    function changeGood(obj){
	    	if(confirm("确定更换商品吗？")){
	    		var num=prompt("请输入购买数量：","1");
		    	var detailId=$(obj).parent().parent().find("td").eq(0).text();
		    	var itemId=$(obj).parent().parent().parent().find("tr").eq(2).find("td").eq(0).text();
		    	var orderTotId=$(obj).parent().parent().parent().find("tr").eq(2).find("td").eq(1).text();
		    	var clazz=$(obj).parent().parent().parent().find("tr").eq(0).find("td").eq(0).text();
		    	var brand=$(obj).parent().parent().parent().find("tr").eq(1).find("td").eq(0).text();
		    	var referencePrice=$(obj).parent().parent().find("td").eq(2).text();
		    	var receipt=referencePrice*num;
		    	var title=$(obj).parent().parent().find("td").eq(1).text();
		    	var color=$(obj).parent().parent().find("td").eq(5).text();
		    	var size=$(obj).parent().parent().find("td").eq(3).text();
		    	var gender=$(obj).parent().parent().find("td").eq(6).text();
		    	var age=$(obj).parent().parent().find("td").eq(4).text();
		    	$.ajax({
	    			url:'/ClothesShopPOS/orderitem/update',
	    			type : "POST",
					cache : false,
					dataType:"json",
					data:{
						id:itemId,
						orderTotId:orderTotId,
						detailId:detailId,
						receipt:receipt,
						referencePrice:referencePrice,
						title:title,
						clazz:clazz,
						brand:brand ,
						color:color,
						size:size,
						gender:gender,
						age:age,
						status:"已完成",
						num:num,
						promotionId:""
					}, 
					success:function(data){
						console.log(data);
	                    //data = JSON.parse(data);
					    if (data.statusCode=='0'){
					    	 alert("换货成功");
					    }else if(data.statusCode=='11'){
					    	alert("您的权限不够，不能执行此操作！");
						}else{
					    	 alert("换货失败");
					    }
					    
					    $("#showOrderItem").modal('hide');
					    $("#chooseGoodToChange").modal('hide');
					    resetOrderItemList();
					    resetgoodDetailList();
					}
	    		})
	    	}
	    	
	    }
	</script>
  </body>
</html>
